package duan.controller;

import duan.constant.RespConstant;
import duan.entity.RespHelper;
import duan.entity.ResultVO;
import duan.entity.User;
import duan.http.HttpConnUtil;
import duan.util.HttpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping(value = "/user", produces = "application/json;charset=utf-8")
public class UserController {

    private static final String check_url = "http://localhost:8080/user/checkUser.do";

    @Resource
    private HttpConnUtil httpConnUtil;

    @PostMapping(value = "/getUserName.do")
    public ResultVO<User> getUserName(String userName) {
        User user = new User();
        try {
            user.setUserName(userName);
        } catch (Exception e) {
            e.printStackTrace();
            return RespHelper.error(RespConstant.EXCEPTION_ERROR_MSG);
        }
        return RespHelper.success(user);
    }

    @PostMapping(value = "/setUserName.do")
    public ResultVO<String> setUserName(String userName) {
        String result = null;
        try {
            User user = new User();
            user.setUserName(userName);
            String url = HttpUtil.getUrl(check_url, user);
            System.out.println(url);
            result = httpConnUtil.doGet(url);
        } catch (Exception e) {
            e.printStackTrace();
            return RespHelper.error(RespConstant.EXCEPTION_ERROR_MSG);
        }
        return RespHelper.success(result);
    }

    @GetMapping(value = "/checkUser.do")
    public ResultVO<Boolean> checkUser(User user) {
        boolean pass = false;
        try {
            if ("duan".equals(user.getUserName())) {
                pass = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RespHelper.error(RespConstant.EXCEPTION_ERROR_MSG);
        }
        return RespHelper.success(pass);
    }



}
